/*
 * @Author: HX
 * @Date: 2024-12-05 14:09:57
 * @LastEditors: HX
 * @LastEditTime: 2024-12-16 15:14:59
 */
import global from '../../core/global.js';
import { Model, UEModel } from '../model.js';
import { Tiles } from '../tiles.js';
import { Label3D } from '../label.js';
import { Pipeline } from '../pipeline.js';
import { Animation } from '../track-animation.js';
import { Billboard } from '../billboard.js';
import { convertColorFormat, log2ssmapObj } from '../../core/common.js';
import { showLayerByLayer, cleanTestLayer } from './handle-layer.js';

/**
 * 参数 调用
 * @description 加载轨迹动画模型
 * @return animation id
 */
export function addAnimation(opt, cb) {
	Object.assign(opt, opt.modelInfo || {});
	const hasId = opt.id;
	// 已经加载则不重复加载 改用显隐
	let needReload = showLayerByLayer(opt, 'delete');
	if (!needReload) {
		cb && cb(opt.id);
		return;
	}
	Animation.add(opt, (tempId) => {
		if (hasId && hasId != 'temp_id_12138') {
			let animation = Animation._getAnimation(opt.id);
			global.layerList.add({ type: 'animation', animation }, opt.id);
			cb && cb(opt.id);
		} else {
			cleanTestLayer();
			let animation = Animation._getModel(opt);
			if (animation) animation.id = 'temp_id_12138';
			global.testLayer = animation;
			cb && cb(tempId);
		}
	});
}

/**
 * 参数 调用
 * @description 加载fbx/obj/gltf/glb等模型数据
 * @return model id
 */
export function addModel(opt, cb) {
	Object.assign(opt, opt.modelInfo || {});
	const hasId = opt.id;
	// 已经加载则不重复加载 改用显隐
	let needReload = showLayerByLayer(opt, 'delete');
	if (!needReload) {
		cb && cb(opt.id);
		return;
	}
	let modelFunc = opt.url.includes('sstf') ? UEModel : Model;
	modelFunc.add(opt, (tempId) => {
		if (hasId && hasId != 'temp_id_12138') {
			let model = Model._getModel(opt);
			global.layerList.add({ type: 'model', model }, opt.id);
			cb && cb(opt.id);
		} else {
			cleanTestLayer();
			let model = Model._getModel(opt);
			if (model) model.id = 'temp_id_12138';
			global.testLayer = model;
			cb && cb(tempId);
		}
	});
}

/**
 * 参数 调用
 * @description 加载3dtiles数据
 * @return
 */
export function addTiles(opt, cb) {
	Object.assign(opt, opt.modelInfo || {});
	const hasId = opt.id;
	// 已经加载则不重复加载 改用显隐
	let needReload = showLayerByLayer(opt, 'delete');
	if (!needReload) {
		cb && cb(opt.id);
		return;
	}
	Tiles.add(opt, (tempId) => {
		if (hasId && hasId != 'temp_id_12138') {
			let tiles = Tiles._getTiles(opt);
			global.layerList.add({ type: 'tiles', tiles }, opt.id);
			cb && cb(opt.id);
		} else {
			cleanTestLayer();
			let tiles = Tiles._getTiles(opt);
			tiles.id = 'temp_id_12138';
			global.testLayer = tiles;
			cb && cb(tempId);
		}
	});
}

/**
 * 参数 调用
 * @description 加载Label3D
 * @return
 */
export function addLabel3D(opt, cb) {
	Object.assign(opt, opt.modelInfo || {});
	const hasId = opt.id;
	// 已经加载则不重复加载 改用显隐
	let needReload = showLayerByLayer(opt, 'enabled');
	if (!needReload) {
		cb && cb(opt.id);
		return;
	}
	Label3D.add(opt, (tempId) => {
		if (hasId && hasId != 'temp_id_12138') {
			let label = Label3D._getLabel(opt.id);
			label.type = 'label';
			global.layerList.add(label, opt.id);
			cb && cb(opt.id);
		} else {
			cleanTestLayer();
			let label = Label3D._getLabel(opt.id);
			label.id = 'temp_id_12138';
			global.testLayer = label;
			cb && cb(tempId);
		}
	});
}

/**
 * 参数 调用
 * @description 加载Pipeline
 * @return
 */
export function addPipeline(opt, cb) {
	Object.assign(opt, opt.modelInfo || {});
	const hasId = opt.id;
	// 已经加载则不重复加载 改用显隐
	let needReload = showLayerByLayer(opt, 'enabled');
	if (!needReload) {
		cb && cb(opt.id);
		return;
	}
	Pipeline.add(opt, (tempId) => {
		if (hasId && hasId != 'temp_id_12138') {
			let pipeline = Pipeline._getPipeline(opt.id);
			pipeline.type = 'pipeline';
			global.layerList.add(pipeline, opt.id);
			cb && cb(opt.id);
		} else {
			cleanTestLayer();
			let pipeline = Pipeline._getPipeline(opt.id);
			pipeline.id = 'temp_id_12138';
			global.testLayer = pipeline;
			cb && cb(tempId);
		}
	});
}
/**
 * 参数 调用
 * @description 加载Billboard
 * @return
 */
export function addBillboard(opt, cb) {
	Object.assign(opt, opt.modelInfo || {});
	const hasId = opt.id;
	// 已经加载则不重复加载 改用显隐
	let needReload = showLayerByLayer(opt, 'delete');
	if (!needReload) {
		cb && cb(opt.id);
		return;
	}
	Billboard.add(opt, (tempId) => {
		if (hasId && hasId != 'temp_id_12138') {
			let bbEntity = Billboard._getBillboard(opt.id);
			global.layerList.add({ type: 'Billboard', bbEntity }, opt.id);
			cb && cb(opt.id);
		} else {
			cleanTestLayer();
			let bbEntity = Billboard._getBillboard(opt.id);
			bbEntity.id = 'temp_id_12138';
			global.testLayer = bbEntity;
			cb && cb(tempId);
		}
	});
}
